import grpc
import user_service_pb2
import user_service_pb2_grpc

def run():
    # 连接到 gRPC 服务
    channel = grpc.insecure_channel('localhost:50051')
    stub = user_service_pb2_grpc.UserServiceStub(channel)
    
    # 用户注册测试
    register_request = user_service_pb2.RegisterRequest(
        username="new_user", 
        password="new_password", 
        email="new_user@example.com"
    )
    response = stub.Register(register_request)
    print("Registration successful:", response.success)
    print("Message:", response.message)
    
    # 尝试使用相同的用户名注册
    duplicate_register_request = user_service_pb2.RegisterRequest(
        username="new_user", 
        password="another_password", 
        email="duplicate_user@example.com"
    )
    response = stub.Register(duplicate_register_request)
    print("Registration successful:", response.success)
    print("Message:", response.message)
    
    # 尝试使用相同的邮箱注册
    duplicate_email_register_request = user_service_pb2.RegisterRequest(
        username="new_user2", 
        password="password2", 
        email="new_user@example.com"
    )
    response = stub.Register(duplicate_email_register_request)
    print("Registration successful:", response.success)
    print("Message:", response.message)
    
    # 登录测试
    login_request = user_service_pb2.LoginRequest(username="new_user", password="new_password")
    response = stub.Login(login_request)
    print("Login successful:", response.success)
    
    # 检查是否登录
    is_logged_in = stub.IsLogin(user_service_pb2.Empty())
    print("Is logged in:", is_logged_in.success)
    
    # 退出登录
    stub.Logout(user_service_pb2.Empty())
    print("Logged out.")
    
    # 再次检查是否登录
    is_logged_in = stub.IsLogin(user_service_pb2.Empty())
    print("Is logged in after logout:", is_logged_in.success)

if __name__ == '__main__':
    run()